package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.model.admin.vo.AdReadTopicVo;
import com.cskaoyan.model.admin.vo.AdTopicReadVo;
import com.cskaoyan.model.admin.bo.ListBo;
import com.cskaoyan.model.admin.vo.AdBaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.utils.CouponUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author jiaolei
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public AdBaseData list(ListBo listBo, MarketTopic marketTopic) {
        PageHelper.startPage(listBo.getPage(),listBo.getLimit());

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        if(marketTopic.getTitle()!=null||marketTopic.getSubtitle()!=null){
            MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
            if(marketTopic.getTitle()!=null){
                criteria =criteria.andTitleLike(CouponUtil.addHundred(marketTopic.getTitle()));
            }
            if(marketTopic.getSubtitle()!=null){
                criteria=criteria.andSubtitleLike(CouponUtil.addHundred(marketTopic.getSubtitle()));
            }
        }

        marketTopicExample.setOrderByClause(listBo.getSort()+" "+listBo.getOrder());
        List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);

        PageInfo pageInfo = new PageInfo(marketTopics);
        long total = pageInfo.getTotal();

        Integer pages=null;
        if(listBo.getLimit()!=null&& listBo.getLimit()!=0){
            pages=marketTopics.size()/ listBo.getLimit()+1;
        }

        return AdBaseData.list(marketTopics, listBo.getPage(), listBo.getLimit(),total,pages);
    }


    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        int i = topicMapper.updateByPrimaryKeySelective(marketTopic);
        MarketTopic marketTopic1 = topicMapper.selectByPrimaryKey(marketTopic.getId());
        return marketTopic1;
    }

    @Override
    public Integer delete(MarketTopic marketTopic) {
        int i = topicMapper.deleteByPrimaryKey(marketTopic.getId());
        return i;
    }



    //    @Override
//    public AdTopicReadVo read(Integer id) {
////
//        AdTopicReadVo adTopicReadVo = new AdTopicReadVo();
//        AdTopicReadVo.TopicBean topicBean=topicMapper.selectTopicBeanByPrimaryKey(id);
//        adTopicReadVo.setGoodsList(new ArrayList<Integer>());
//        adTopicReadVo.setTopic(topicBean);
//        return adTopicReadVo;
//    }
    @Override
    public MarketTopic create(MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        int i = topicMapper.insertSelective(marketTopic);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.createCriteria().andTitleEqualTo(marketTopic.getTitle());
        List<MarketTopic> marketTopics = topicMapper.selectByExample(marketTopicExample);

        return marketTopics.get(0);
    }

    @Override
    public int batchDelete(Map<Object,Integer[]> map) {
        Integer[] ids = (Integer[]) map.get("ids");
        int i=0;
        for (Integer id : ids) {
            i+= topicMapper.deleteByPrimaryKey(id);
        }
        return i;
    }

    @Override
    public AdReadTopicVo topicRead(Integer id) {
        AdReadTopicVo adReadTopicVo = new AdReadTopicVo();
        List<MarketGoods> marketGoods = new ArrayList<>();
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        Integer[] goods = marketTopic.getGoods();
        for (Integer goodId : goods) {
            marketGoods.add(marketGoodsMapper.selectByPrimaryKey(goodId));
        }
        adReadTopicVo.setGoodsList(marketGoods);
        adReadTopicVo.setTopic(marketTopic);
        return adReadTopicVo;
    }
}
